export const REG_MAP = {
  phone: /^1\d{10}$/
}

type KeyOfRegType = keyof typeof REG_MAP

export function validReg(type: KeyOfRegType | 'required', message = '请输入', trigger: 'onBlur' | 'onChange' = 'onBlur') {
  if (type === 'required') {
    return { required: true, message, trigger }
  }
  return {
    validator: (value: string | number) => {
      if (!value && value !== 0) {
        return true
      }
      return REG_MAP[type].test(value + '')
    },
    message,
    trigger
  }
}

export function validRange(range: [number, number], message: string, trigger: 'onBlur' | 'onChange' = 'onBlur') {
  return {
    validator: (value: string | number) => {
      if (!value && value !== 0) {
        return true
      }
      const num = Number(value)
      return !(num < range[0] || num > range[1])
    },
    message: message || `有效范围${range[0]}-${range[1]}`,
    trigger
  }
}
